﻿package org.atk.components.treegridview {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import org.aswing.ASColor;
	import org.aswing.AssetIcon;
	import org.aswing.geom.IntRectangle;
	
	/**
	 * ...
	 * @author AeTky
	 */
	public class TreeGridViewRenderer {
		
		public function TreeGridViewRenderer(){
			
		}
		
		protected function textOut(sprite:Sprite,bounds:IntRectangle,txtOut:*):TextField {
			var txt:String = "" + txtOut;
			var lb:TextField = new TextField();
			
			lb.selectable = false;
			lb.autoSize = TextFieldAutoSize.NONE;
			lb.text = txt;
			
			lb.x = bounds.x;
			lb.y = bounds.y;
			lb.height = bounds.height;
			lb.width =  bounds.width;
			lb.mouseEnabled = false;
			sprite.addChild(lb);
			return lb;
		}
		protected function drawIcon(sprite:Sprite, bounds:IntRectangle,icn:AssetIcon):void {
			if (icn == null) return;
			var dsp:DisplayObject = icn.getAsset();
			dsp.x = bounds.x;
			dsp.y = bounds.y + (bounds.height - dsp.height) / 2;
			sprite.addChild(dsp);
		}
		public function renderColumnHeaderContent(sprite:Sprite,bounds:IntRectangle,column:TreeGridViewColumn):void{
			textOut(sprite, bounds, column.Value);
		}
		public function renderColumnHeaderBox(sprite:Sprite,bounds:IntRectangle,column:TreeGridViewColumn):void{
			var hBgColor:ASColor = ASColor.WHITE;
			var hLnColor:ASColor = ASColor.BLACK;
			if(column.HeaderBgColor!=null)
				hBgColor = column.HeaderBgColor;
				
			if(column.HeaderBorderColor!=null)
				hLnColor = column.HeaderBorderColor;
			
			sprite.graphics.lineStyle(1, hLnColor.getRGB());
			sprite.graphics.beginFill(hBgColor.getRGB());
			sprite.graphics.drawRect(bounds.x, bounds.y,bounds.width,bounds.height);
			sprite.graphics.endFill();
			sprite.graphics.lineStyle(undefined);
		}
		public function renderColumnBg(sprite:Sprite,bounds:IntRectangle,column:TreeGridViewColumn):void{
			if (column.ColumnBgColor == null && column.ColumnBorderColor == null)
				return;
			
			var hBgColor:ASColor = null;
			var hLnColor:ASColor = null;
			var widthPatch:int = 0;
			
			if(column.ColumnBgColor!=null)
				hBgColor = column.ColumnBgColor;
			if(column.ColumnBorderColor!=null)
				hLnColor = column.ColumnBorderColor;
			if(hLnColor!=null)
				sprite.graphics.lineStyle(1, hLnColor.getRGB());
			else{
				widthPatch = 1;
				sprite.graphics.lineStyle(undefined);
			}
			
			if(hBgColor!=null)
				sprite.graphics.beginFill(hBgColor.getRGB());
			
			sprite.graphics.drawRect(bounds.x, bounds.y, bounds.width+widthPatch, bounds.height);
			
			if(hBgColor!=null)
				sprite.graphics.endFill();
			
			sprite.graphics.lineStyle(undefined);
		}
		
		public function renderCellContent(sprite:Sprite, bounds:IntRectangle, columnIdx:int, item:TreeGridViewItem):void {
			textOut(sprite, bounds, item.Data.getValue(columnIdx));
		}
		
	}
	
}